package com.example.project01.service;

import com.example.project01.model.Feedback;
import com.leon.model.Model;
import com.leon.model.Pageable;
import com.leon.model.Paged;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
@AllArgsConstructor
public class FeedbackService {

    private final SqlSession sqlSession;

    public int save(Feedback feedback, boolean update) {
        if (update) {
            feedback.setState(Feedback.StateEnum.RESOLVED.name());
            return sqlSession.insert("feedback.insertFeedback", feedback.toMap());
        }
        feedback.setState(Feedback.StateEnum.UNSETTLED.name());
        return sqlSession.insert("feedback.insertFeedback", feedback.toMap());
    }

    public Paged<Feedback> feedback(Pageable pageable, String liaison, String state, Timestamp reactionAt, Timestamp solveAt) {
        int total = sqlSession.selectOne("feedback.wholeFeedback");
        Map<String, Object> map = pageable.asMap().put("liaison", liaison).put("state", state).put("state", state).put("reactionAt", reactionAt).put("solveAt", solveAt).end();
        List<Feedback> feedbacks = Model.converts(Feedback::new, sqlSession.selectList("feedback.listFeedback", map));
        return Paged.of(feedbacks, total);
    }

}